//
//  DatabaseViewController.m
//  Database
//
//  Created by Allan Kuan on 12-07-06.
//  Copyright (c) 2012 UGuideUs. All rights reserved.
//
//  Base code structure from Techotopia, with modifications.

#import "CommentViewController.h"

@interface CommentViewController()
@end

@implementation CommentViewController

// Initalize the class variables.
@synthesize location;
@synthesize displayAllComments;

//picker
@synthesize picker, placeNames, pickerBar,showPicker;
@synthesize result;

- (void) viewDidLoad 
{
    [super viewDidLoad];
    
    //change navigation bar colour
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:172.0/255   
                                                                        green:04.0/255   
                                                                         blue:08.0/255   
                                                                        alpha:1];  
     //picker
    self.placeNames = [[NSArray alloc] initWithObjects:
                       @"Bamboo Garden",
                       @"Booster Juice",
                       @"Bubble World",
                       @"Club Ilia",
                       @"Diamond Alumni Centre",
                       @"Donair Town",
                       @"Higher Grounds Coffee and Highland Pub",
                       @"Himalayan Peak",
                       @"Ichibankan Express Sushi",
                       @"Koya Japan",
                       @"The Ladle",
                       @"Mackenzie Cafe",
                       @"Mr.Sub",
                       @"Nature's Garden Organic Deli",
                       @"Pizza Point",
                       @"Renaissance Coffee",
                       @"Romeo and Juliet",
                       @"Simon C's Convenience Store",
                       @"Spicy Stone",
                       @"Tim Hortons",
                       @"Triple O's",
                       @"Temporary Public Parking 1",
                       @"Parking Lot G",
                       @"Parking Lot B",
                       @"Visitor Parking B Lot",
                       @"Visitor Parking C Lot",
                       @"Visitor Parking N Lot",
                       @"Reserved Resident Parking",
                       @"Parking Lot C",
                       @"Visitor's Parkade",
                       @"Parking Lot F",
                       @"Parking Lot D",
                       @"Parking Lot E",
                       @"The Hub and Cornerstone Parking",
                       @"Parking Lot H",
                       @"Parking Lot P3",
                            nil];
}

- (void) viewDidUnload
{
    [self setLocation:nil];
    [super viewDidUnload];
    
    // Release any retained subviews of the main view.

    self.location = nil;
  
    
    //picker
    self.picker = nil;
}


//picker datasource
- (NSInteger)numberOfComponentsInPickerView:
(UIPickerView *)picker
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)picker
numberOfRowsInComponent:(NSInteger)component
{
    return [placeNames count];
}
- (NSString *)pickerView:(UIPickerView *)picker
             titleForRow:(NSInteger)row
            forComponent:(NSInteger)component
{
    return [placeNames objectAtIndex:row];
} 

//picker delegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
      inComponent:(NSInteger)component
{

    result = [placeNames objectAtIndex:row];
    location.text = result;

}


//picker hide/reveal
-(IBAction)hidePicker:(id)sender{
    picker.hidden=YES;
    pickerBar.hidden=YES;
}

-(IBAction)revealPicker:(id)sender{

    picker.hidden=NO;
    pickerBar.hidden=NO;

}

- (IBAction)getTheCommentsForSpecificLocation:(id)sender {
    
    //Get a new databassAccessor
    DatabaseAccessor *getComment=[[DatabaseAccessor alloc]init];
    [getComment setTheLocation:location.text];
    
    //get the comments of this locations from database
    NSString *jsonresult=[getComment searchComments];
  
    //fiter other useless information by using distinguish ] signal
    NSArray *filter=[jsonresult componentsSeparatedByString:@"]"];
    NSString *jasonAfterFilter=[filter objectAtIndex:0];
    //reconstruct the string by adding ] into JSON format
    jasonAfterFilter=[jasonAfterFilter stringByAppendingString:@"]"];

//   NSLog(@"Filter after recrived data:%@",jasonAfterFilter);
   
    NSArray *JASONResults = [jasonAfterFilter JSONValue];
    NSLog(@"JAON results:%@",JASONResults);
    
    NSString *resultToBePrinted=@"";
    for(NSDictionary *aComment in JASONResults){
//        NSLog(@"username=%@;date=%@;\r\n comment:%@\r\n", [aComment objectForKey:@"username"], [aComment objectForKey:@"date"],[aComment objectForKey:@"comment"]);
        resultToBePrinted=[resultToBePrinted stringByAppendingString:[NSString stringWithFormat:@"Username: %@;\r\nDate: %@;\r\n%@\r\n\r\n", [aComment objectForKey:@"username"], [aComment objectForKey:@"date"],[aComment objectForKey:@"comment"]]];
    }
    if(resultToBePrinted.length==0){
        self.displayAllComments.text=@"Comments cannot be found, please kindly add the comment! Thanks!";
    }else{
        self.displayAllComments.text=resultToBePrinted;
    }
}

/*  NAME:       prepareForSegue()
 *
 *  PURPOSE:    Allows another view to prepare for the sending of information from this view.
 */

- (void)prepareForSegue : (UIStoryboardSegue *)segue sender : (id)sender
{
    if ([segue.identifier isEqualToString : @"theLocationForComment"]) 
    { 
        NSLog(@"location.text=%@",location.text);
        [segue.destinationViewController setLocationName:location.text];
    }
}

- (IBAction)LocationToAddComment:(UIButton *)sender {
    if([location.text isEqualToString : @"Select An Amenity"]){
        displayAllComments.text=@"You shold choose the location from the list!Thanks!";
    }else{
    [self performSegueWithIdentifier : @"theLocationForComment" sender : self];
    }
}

@end